//
//  CountScoreViewController.m
//  NewGame
//
//  Created by wwj on 15/10/22.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "CountScoreViewController.h"
#import "CountScoreView.h"
#import "StageModel.h"
#import "StageRecordModel.h"
#import "FailView.h"
#import "RecordNewView.h"
#import "StageRecordTool.h"
#import "StagesViewController.h"

@interface CountScoreViewController ()
@property (weak ,nonatomic) IBOutlet CountScoreView *countScoreView;
@property (weak, nonatomic) IBOutlet UIImageView *rankImg;
@property (weak, nonatomic) IBOutlet UIImageView *bgImage;


@end

@implementation CountScoreViewController

-(void)setScoreNew:(double)scoreNew{
    _scoreNew = scoreNew;
}

-(void)setStageModel:(StageModel *)stageModel{
    _stageModel = stageModel;
}

+(instancetype)countScoreViewController{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    CountScoreViewController *countScoreVC = [storyboard instantiateViewControllerWithIdentifier:@"countScore"];
    return countScoreVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [_countScoreView countScoreWithNewScore:_scoreNew model:_stageModel];
    
    _countScoreView.rankBlock = ^(NSString *rank){
        _rankImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"score_%@",rank]];
    };
    
    _countScoreView.showResultBlock = ^(NSString *rank){
        BOOL betterScore = ((_stageModel.max > _stageModel.min) && (_scoreNew > _stageModel.recordModel.score)) ||
        ((_stageModel.max < _stageModel.min) && (_scoreNew < _stageModel.recordModel.score));
        
        BOOL needSaveData = (_stageModel.recordModel.rank == nil) || betterScore;
        
        if (needSaveData) {
            //保存数据
            [self saveData:rank];
        }
        
        if ([@"f" isEqualToString:rank]) {//失败
            [self showFailView];
            
        }else{
            if (needSaveData) {//新记录
                [self showNewRecord];
            }else{
                if ([@"s" isEqualToString:rank]) {//最好
                    [[SoundTool sharedSoundTool] playSound:kSoundNormalGrade];
                    [[SoundTool sharedSoundTool] playSound:kSoundSGrade];
                }else{//普通  F ~ A
                    [[SoundTool sharedSoundTool] playSound:kSoundNormalGrade];
                }
            }
            
        }
    };
}

#pragma -mark 保存数据
- (void)saveData:(NSString *)rank{
    _stageModel.recordModel.rank = rank;
    _stageModel.recordModel.score = _scoreNew;
    [[StageRecordTool sharedStageRecordTool] saveStageRecord:_stageModel.recordModel];
    
    //检测下一关
    if (![@"f" isEqualToString:rank]) {
        StageRecordModel *next = [[StageRecordTool sharedStageRecordTool] stageRecordWithNo:_stageModel.no + 1];
        if (next == nil) {
            next = [[StageRecordModel alloc] init];
            next.no = _stageModel.no + 1;
            [[StageRecordTool sharedStageRecordTool] saveStageRecord:next];
        }
    }
   
}

#pragma -mark 新纪录
- (void)showNewRecord{
    _bgImage.image = [UIImage imageNamed:@"scene_bg"];
    RecordNewView *recordNewView = [RecordNewView recordNewView];
    [self.view insertSubview:recordNewView aboveSubview:_bgImage];
    [recordNewView begin];
}

#pragma -mark 显示失败页面
- (void)showFailView{
    FailView *failView = [FailView failView];
    [failView setScore:_scoreNew minScore:_stageModel.min];
    [self.view addSubview:failView];
    [failView begin];
}

- (IBAction)retry{
    [self.navigationController popViewControllerAnimated:YES];
    // 2.通知关卡界面重新开始游戏
    [[NSNotificationCenter defaultCenter] postNotificationName:@"restartGame" object:nil];
}

- (IBAction)selectOther{    
    UINavigationController *navigation = self.navigationController;
    StagesViewController *stagesVC = navigation.viewControllers[1];
    [stagesVC reloadDataWithNO:_stageModel.no];
    [self.navigationController popToViewController:stagesVC animated:YES];
}

@end
